﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ClassBuilder.Comment
{
    /// <summary>
    /// 代码注释类
    /// </summary>
    public class CSComment
    {
        /// <summary>
        /// 代码注释
        /// </summary>
        /// <param name="desc">描述内容</param>
        public CSComment(string desc)
        {
            this.Desc = desc;
        }

        /// <summary>
        /// 代码注释
        /// </summary>
        /// <param name="desc">描述内容</param>
        /// <param name="itemType">注释项的Style</param>
        /// <param name="items">注释项内容</param>
        public CSComment(string desc, ItemType itemType, params string[] items)
        {
            this.Desc = desc;
            this.Items = new ListItems(itemType, items);
        }

        /// <summary>
        /// 描述内容
        /// </summary>
        public string Desc { get; set; }

        /// <summary>
        /// 段落集合
        /// </summary>
        public List<string> Paras { get; set; } = new List<string>();

        /// <summary>
        /// 列表内容
        /// </summary>
        public ListItems Items { get; set; } = new ListItems();

        /// <summary>
        /// 示例内容
        /// </summary>
        public Example Exp { get; set; }


        /// <summary>
        /// 最终显示内容
        /// </summary>
        public string Value
        {
            get
            {
                return this.ToString();
            }
        }

        /// <summary>
        /// 所有行注释
        /// </summary>
        public string[] Lines
        {
            get
            {
                return this.Value?.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

        /// <summary>
        /// 根据tab处理tab间隔
        /// </summary>
        /// <param name="tab_space">tab间隔个数</param>
        /// <returns></returns>
        public string ByTabs(int tab_space)
        {
            StringPlus plus = new StringPlus();
            foreach (var line in Lines)
            {
                plus.AppendSpaceLine(tab_space, line);
            }
            return plus.ToString();
        }


        public void AddPara(string para)
        {
            if (this.Paras == null)
            {
                this.Paras = new List<string>();
            }
            this.Paras.Add(para);
        }

        public void AddItem(string item)
        {
            if (this.Items == null)
            {
                this.Items = new ListItems();
            }
            this.Items.Items.Add(item);
        }


        /// <summary>
        /// remarks 用于添加有关某个类型的信息
        /// </summary>
        public string RemarksContent { get; set; }

        /// <summary>
        /// remarks包裹的内容
        /// </summary>
        public string Remarks
        {
            get
            {
                if (RemarksContent.NotEmpty())
                {
                    StringPlus plus = new StringPlus();
                    plus.AppendLine("/// <remarks>");
                    plus.AppendLine($"/// {RemarksContent.HtmlHandle()}");
                    plus.AppendLine("/// </remarks>");
                    return plus.ToString();
                }
                return null;
            }
        }


        public override string ToString()
        {
            StringPlus plus = new StringPlus();
            plus.AppendLine("/// <summary>");

            if (Desc.NotEmpty())
            {
                plus.AppendLine(Desc.FormatSummary());
            }

            if (Paras != null && Paras.Any())
            {
                foreach (var para in Paras)
                {
                    plus.AppendLine($"/// <para>{para.Trim().HtmlHandle()}</para>");
                }
            }

            if (Items != null && Items.Items.Any())
            {
                plus.AppendLine(Items.ToString());
            }

            plus.AppendLine("/// </summary>");

            if (Exp != null && Exp.ToString().NotEmpty())
            {
                plus.AppendLine(Exp.ToString());
            }

            if (Remarks.NotEmpty())
            {
                plus.AppendLine(Remarks);
            }
            return plus.ToString().Trim();
        }
    }
}
